package com.bjsxt.search.service.impl;

import com.bjsxt.search.dao.ItemDao4ES;
import com.bjsxt.search.pojo.Item4ES;
import com.bjsxt.search.service.SearchService;
import com.livegoods.commons.vo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : SearchServiceImpl
 * @Description :
 * @Author : ZWZ
 * @Date: 2021/8/24 16:42
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ItemDao4ES itemDao4ES;

    /**
     * 搜索商品逻辑
     *
     * @param city    城市
     * @param content 搜索关键字
     * @param page    第几页， 从 0 开始
     * @param size    每页查询多少行
     * @return
     */
    @Override
    public LivegoodsResult search(String city, String content, int page, int size) {
        // es 搜索
        AggregatedPage<Item4ES> item4ESAggregatedPage = itemDao4ES.queryForPage(city, content, page, size);
        // 获取搜索结果
        List<Item4ES> item4ESList = item4ESAggregatedPage.getContent();
        // 组装返回
        LivegoodsResult result = LivegoodsResult.ok(item4ESList);
        result.setHasMore(page < (item4ESAggregatedPage.getTotalPages() - 1));
        return result;
    }

}
